// Copyright (c) Runtime Verification, Inc. All Rights Reserved.
package org.kframework.utils;

import com.google.inject.Inject;
import java.util.Formatter;
import org.kframework.main.GlobalOptions;
import org.kframework.utils.inject.RequestScoped;

@RequestScoped
public class Stopwatch {
  private final long start;
  private long lastIntermediate;
  Formatter f = new Formatter(System.out);
  private final GlobalOptions options;

  @Inject
  public Stopwatch(GlobalOptions options) {
    this.options = options != null ? options : new GlobalOptions();
    start = System.currentTimeMillis();
    lastIntermediate = start;
  }

  public void start() {
    printIntermediate("Init");
  }

  public void printIntermediate(String message) {
    long current = System.currentTimeMillis();
    if (options.verbose)
      f.format("%-60s = %s%n", message, milisecondsToTime(current - lastIntermediate));
    lastIntermediate = current;
  }

  public void printTotal(String message) {
    printIntermediate("Cleanup");
    if (options.verbose)
      f.format("%-60s = %s%n", message, milisecondsToTime(lastIntermediate - start));
  }

  private static String milisecondsToTime(long miliseconds) {
    long h = miliseconds / 3600000;
    long m = miliseconds % 3600000 / 60000;
    double s = miliseconds % 60000 / 1000.;
    if (h > 0) return String.format("%dh %02dm %02ds", h, m, (long) s);
    if (m > 0) return String.format("%02dm %02ds", m, (long) s);
    return String.format("%6.3fs", s);
  }
}
